package com.zimmur.app.mocolink.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zimmur.app.mocolink.dao.UserAddressMapper;
import com.zimmur.app.mocolink.domain.model.UserAddress;
import com.zimmur.app.mocolink.domain.model.UserAddressExample;
import com.zimmur.app.mocolink.domain.vo.UserAddressInfo;
import com.zimmur.app.mocolink.service.IUserAddressService;

@Service
public class UserAddressServiceImpl implements IUserAddressService {

	@Autowired
	private UserAddressMapper userAddressMapper;
	@Override
	public int add(UserAddress userAddress) {
		
		return userAddressMapper.insertSelective(userAddress);
	}

	@Override
	public int update(UserAddress userAddress) {
		return userAddressMapper.updateByPrimaryKeySelective(userAddress);
	}

	@Override
	public int updateDefault(int addressId, int userId) {
		int flag=0;
		UserAddress userAddress= userAddressMapper.selectByPrimaryKey(addressId);
		if(userAddress!=null){
			 userAddressMapper.cancelDefault(userId);
			 userAddress.setIsDefault((short)1);
			 flag =userAddressMapper.updateByPrimaryKey(userAddress);
		}
		return flag;
	}

	@Override
	public int delete(int addressId) {
		return userAddressMapper.deleteByPrimaryKey(addressId);
	}

	@Override
	public List<UserAddressInfo> selectByUserId(Integer userId) {
		List<UserAddressInfo> list = userAddressMapper.selectByUserId(userId);
		return list;
	}

	@Override
	public UserAddressInfo selectDefaultAddress(Integer userId) {
		List<UserAddressInfo> list = userAddressMapper.selectByUserId(userId);
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public UserAddress selectById(Integer addressId) {
		return userAddressMapper.selectByPrimaryKey(addressId);
	}

}
